package gold.enterprise;

import org.apache.commons.lang.StringUtils;

import java.util.*;

public class CoupangFour {

    public long startTime = System.currentTimeMillis();

    TreeNode rootNode = new TreeNode();   //定义一个敏感词前缀树字典

    //step 1 :定义前缀树结构
    public class TreeNode {

        // 最后一个字的标记位
        private boolean isKeyWordEnd = false;

        //子节点列表
        private Map<Character, TreeNode> subNodes = new HashMap<>();

        public boolean isKeyWordEnd() {
            return isKeyWordEnd;
        }

        public void setKeyWordEnd(boolean keyWordEnd) {
            isKeyWordEnd = keyWordEnd;
        }

        public void addSubNode(Character key, TreeNode node) {
            this.subNodes.put(key, node);
        }

        public TreeNode getSubNode(Character key) {
            return this.subNodes.get(key);
        }
    }

    //前缀树添加敏感词的方法
    public void addSensitiveWord(String word) {
        TreeNode tempNode = rootNode;//从头部遍历
        for (int i = 0; i < word.length(); i++) {
            Character c = word.charAt(i);
            TreeNode node = tempNode.getSubNode(c);

            //当前失配
            if (null == node) {
                node = new TreeNode();
                tempNode.addSubNode(c, node);
            }

            //后移node
            tempNode = node;
            if (i == word.length() - 1) {
                tempNode.setKeyWordEnd(true);
            }
        }
    }


    public String sensitiveWordMatch(String text) {
        if (StringUtils.isBlank(text)) return text;

        StringBuilder result = new StringBuilder();
        TreeNode tempNode = rootNode;
        int begin = 0, position = 0;

        while (position < text.length()) {
            Character c = text.charAt(position);
            tempNode = tempNode.getSubNode(c);
            if (null == tempNode) {
                //匹配失败情况:当前begin开始不存在敏感词
                result.append(String.valueOf(text.charAt(begin)));
                ++begin;
                position = begin;
                tempNode = this.rootNode;
                continue;
            } else if (tempNode.isKeyWordEnd()) {
                //当前已是末尾，需要替换敏感词
                result.append("*");//此处敏感词被替换为单个*
                position++;
                begin = position;
                tempNode = this.rootNode;
                continue;
            } else {
                position++;
            }
        }


        return result.toString();

    }

    public static void main(String[] args) {
        CoupangFour solution = new CoupangFour();

//
//        solution.addSensitiveWord("ab");
//        solution.addSensitiveWord("cd");

        for (int i = 0; i < 200; i++) {
            int randomWordLength = ((int) (Math.random() * 10));
            StringBuilder randomBuilder = new StringBuilder();
            for (int j = 0; j < randomWordLength; j++) {
                Character curNum = (char) ('a' + ((int) (Math.random() * 26)));
                randomBuilder.append(String.valueOf(curNum));
            }
            solution.addSensitiveWord(randomBuilder.toString());
        }

        System.out.println(solution.sensitiveWordMatch("abe - -sldfjslflskdjflsdfl dsf=s-dflnlskdjflksflo87934cd"));
        System.out.println("\ntotal consume: " + (System.currentTimeMillis() - solution.startTime)+" ms.");
    }

}
